<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Cache;
use addons\thirdwork\library\Application;

/**
 * 企业第三方登录管理
 *
 * @icon fa fa-circle-o
 */
class Thirdwork extends Backend
{
    /**
     * Thirdwork模型对象
     * @var \app\admin\model\Thirdwork
     */
    protected $model = null;

    protected $app = null;
    protected $options = [];

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'id,code';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Thirdwork;

        $this->view->assign('platformList', \app\admin\model\Thirdwork::getPlatformList());

        $config = get_addon_config('thirdwork');
        $this->app = new Application($config);
    }

    /**
     * 首页
     */
    public function index()
    {
        return $this->view->fetch();
    }

    /**
     * 获取部门列表
     */
    public function getDepartmentList()
    {
        $platform = $this->request->param('platform');
        if (!$platform) {
            $this->error('未启用任何第三方平台，请到[插件管理]中进行配置。');
        }
        $cache = Cache::get($platform . '_department_list');
        if ($cache && $this->request->param('isSyncDepList') === 'false') {
            if (isset($cache['department_list']) && time() < $cache['expire_time']) {
                $this->success('', null, $cache['department_list']);
            }
        }
        $data = $this->app->{$platform}->getDepartmentList();
        if ($data && $data['code']) {
            // 缓存部门列表10分钟
            Cache::tag('thirdwork')->set($platform . '_department_list', [
                'department_list'   => $data['data'],
                'expire_time'       => time() + 600
            ]);
            $this->success('', null, $data['data']);
        }
        $this->error($data['msg'], null, $data['data']);
    }
    /**
     * 获取部门成员详情列表
     */
    public function getDepartmentUserList()
    {
        $param = $this->request->param();
        if (isset($param['department_id']) && isset($param['platform'])) {
            $platform = $param['platform'];
            $data = $this->app->{$platform}->getDepartmentUserList();
            if ($data && $data['code']) {
                $localUserids = $this->model->where('platform', $platform)->column('openuserid');
                $list = [];
                foreach ($data['data'] as $k => $v) {
                    if (in_array($v['userid'], $localUserids)) {
                        $data['data'][$k]['isbind'] = true;
                    } else {
                        $data['data'][$k]['isbind'] = false;
                    }
                }
                $list['total']  = $this->app->{$platform}->getDepartmentUserCount()['data'];
                $list['rows']   = $data['data'];
                return $list;
            }
            $this->error($data['msg'], null, $data['data']);
        }
        return [];
    }
    /**
     * 本地已绑定的部门成员列表
     */
    public function getLocalDepartmentBindList()
    {
        $platform = $this->request->param('platform');
        $tabletype = $this->request->param('tabletype');
        $department_id = $this->request->param('department_id');
        if (isset($department_id)) {
            // 远端部门成员
            $remoteUserIds = $this->app->{$platform}->getDepartmentUserIds();

            if ($remoteUserIds && $remoteUserIds['code']) {
                // 本地所有成员
                $localUserIds = $this->model->where('platform', $platform)->column('openuserid');
                if ($tabletype === 'isbind') {
                    // 筛选出本地已绑定成员
                    $bindIds = array_intersect($remoteUserIds['data'], $localUserIds);
                } else {
                    $bindIds = $localUserIds;
                }
                $params = request()->get();
                $data = $this->model
                    ->where('platform', $platform)
                    ->where('openuserid', 'in', $bindIds)
                    ->limit($params['offset'], $params['limit'])
                    ->order($params['sort'], $params['order'])
                    ->select();
                $list['total']  = count($bindIds);
                $list['rows']   = $data;
                return $list;
            }
        }
        return [];
    }
}
